package com.itheima.homework11;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/*
	1. File类：递归遍历文件夹 和 递归查看各类文件的数目。
 */
public class Test01 {
    public static void main(String[] args) {
        File file = new File("C:\\Users\\hldn\\Desktop\\测试");
        ArrayList<File> dirList = new ArrayList<>();
        ArrayList<File> fileList = new ArrayList<>();
        diGui(file, dirList, fileList);
        HashMap<String, Integer> hm = new HashMap<>();
        for (int i = 0; i < fileList.size(); i++) {
            File f = fileList.get(i);
            String[] array = f.getPath().split("\\.");
            if (array != null && array.length > 1) {
                String key = array[array.length - 1];
                if (hm.containsKey(key)) {
                    int count;
                    count = hm.get(key);
                    count++;
                    hm.put(key, count);
                } else {
                    hm.put(key, 1);
                }

            }


        }

        Set<Map.Entry<String, Integer>> entries = hm.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
            System.out.println("类型为" + entry.getKey() + "的有" + entry.getValue() + "个");
        }

    }

    public static void diGui(File file, ArrayList<File> dirList, ArrayList<File> fileList) {

        if (file.isFile()) {
            fileList.add(file);
        }
        if (file.isDirectory()) {
            dirList.add(file);
            File[] files = file.listFiles();
            for (File file1 : files) {
                diGui(file1, dirList, fileList);
            }


        }


    }
}
